//
//  Glb_M_VC.m
//  XiAnJMApp
//
//  Created by Kai Wang on 2017/5/22.
//  Copyright © 2017年 Kai Wang. All rights reserved.
//

#import "Glb_M_VC.h"
#import "UIView+customView.h"
#import "MetaDataTool.h"
#import "Stock.h"
#import "RightTableViewCell.h"
#import "Common.h"
#import "First_title.h"
#import "Detail_VC.h"
#import "MenuView.h"
#import "Left_Menu.h"
#import "Nav_Deatil.h"
#import "UDPManager.h"

#define LeftTableViewWidth 100
#define RightLabelWidth 80

#define HEIGHT_NAV    64

@interface Glb_M_VC ()
<

UITableViewDataSource,
First_title_Delegate,
UITableViewDelegate,
HomeMenuViewDelegate,
Nav_Deatil_Delegate,
UIAlertViewDelegate

>

@property (nonatomic ,strong)UIView        * view_Back;
@property (nonatomic ,strong)MenuView      * menu_left;

@property (nonatomic,strong) UITableView *tableV_left;
@property (nonatomic,strong) UITableView *tableV_right;
@property (nonatomic,strong) UIScrollView *buttomScrollView;
@property (nonatomic,strong) NSArray *rightTitles;
@property (nonatomic,strong) NSArray *customStocks;
@property (nonatomic,strong)First_title * view_first_title;
/*
 * 1-自选   2-香港  3-大陆
 */
@property (nonatomic,assign)NSInteger tag_Type;

@property (nonatomic, strong) Nav_Deatil * nav;

@property (nonatomic, strong)UDPManager * manager;

@end

@implementation Glb_M_VC
#pragma mark - 懒加载属性
- (NSArray *)customStocks{
    if (!_customStocks) {
        _customStocks = [MetaDataTool customStocks];
    }
    return _customStocks;
}

-(NSArray *)rightTitles{
    if (!_rightTitles) {
        _rightTitles = @[@"品种", @"最新价", @"涨跌", @"涨跌幅", @"买一", @"买量", @"卖一", @"卖量",@"成交量",@"持仓量",@"成交额"];
    }
    return _rightTitles;
}


#pragma mark - 设置主页面
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setProperty];

    [self createUI];
    
    [self LoadData];
    
    
}
#pragma mark==================setProperty======================
-(void)setProperty{
    
    
    self.k_NavBar_Hidden=YES;
    /*
     * 此页面关闭手势策划
     */
    self.k_Pop_Disabled=YES;
    
    _tag_Type=1;
    self.view .backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }

   
    
}


-(void)createUI{
    self.title=@"全球行情";
    self.view.backgroundColor=[UIColor  whiteColor];
    
    
    _view_Back=[[UIView alloc]init];
    [self.view addSubview:_view_Back];
    [_view_Back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(HEIGHT_NAV);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);

    }];
    
    _nav=[[Nav_Deatil alloc]init];
    [self.view addSubview:_nav];
    [_nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(HEIGHT_NAV);
    }];
    _nav.delegate=self;
    
    
    _view_first_title=[[First_title alloc]init];
    [self.view addSubview:_view_first_title];
    _view_first_title.delegate=self;
    [_view_first_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nav.mas_bottom).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(35);
    }];
    
    [self loadLeftTableView];
    [self loadRightTableView];

    
    /*
     * 加载左侧的滑动菜单
     */
    Left_Menu *leftM = [[Left_Menu alloc]initWithFrame:CGRectMake(0, HEIGHT_NAV, ScreenWidth * 0.5, ScreenHeight-HEIGHT_NAV)];
    leftM.customDelegate = self;
    MenuView *menu = [MenuView MenuViewWithDependencyView:self.view MenuView:leftM isShowCoverView:YES];
    self.menu_left = menu;
    

    
}

/*
 * 加载UDP请求数据
 */
-(void)LoadData{
    _manager=[UDPManager sharedSocketManager];
    [_manager connectToServer];
}




-(void)LeftMenuViewClick:(NSInteger)tag{
    [self.menu_left hidenWithAnimation];
    NSString *tagstr ;
    
    if (tag==5) {
        tagstr=@"你确定退出登录吗？";
        [[[UIAlertView alloc] initWithTitle:@"提示" message:tagstr delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil] show];
    }
    
    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [self.navigationController  popToRootViewControllerAnimated:NO];
    }else{
        
    }
}



//设置分割线顶格
- (void)viewDidLayoutSubviews{
    [self.tableV_left setLayoutMargins:UIEdgeInsetsZero];
    [self.tableV_right setLayoutMargins:UIEdgeInsetsZero];
}
/*
 * 加载左边的TableView
 */
- (void)loadLeftTableView{
  
    self.tableV_left = [[UITableView alloc] init];
    self.tableV_left.delegate = self;
    self.tableV_left.dataSource = self;
    self.tableV_left.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableV_left];
    
    [self.tableV_left mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(35+HEIGHT_NAV);
        make.left.and.bottom.equalTo(self.view);
        make.width.equalTo(@(LeftTableViewWidth));
        
    }];
}

- (void)loadRightTableView{
    self.tableV_right = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.rightTitles.count * RightLabelWidth + 20, ScreenHeight-35-HEIGHT_NAV) style:UITableViewStylePlain];
    self.tableV_right.delegate = self;
    self.tableV_right.dataSource = self;
    self.tableV_right.showsVerticalScrollIndicator = NO;
    
   
    
    self.buttomScrollView = [[UIScrollView alloc] init];
    
    self.buttomScrollView.contentSize = CGSizeMake(self.tableV_right.bounds.size.width, 0);
    self.buttomScrollView.backgroundColor = [UIColor clearColor];
    self.buttomScrollView.bounces = NO;
    self.buttomScrollView.showsHorizontalScrollIndicator = NO;
    
    [self.buttomScrollView addSubview:self.tableV_right];
    [self.view addSubview:self.buttomScrollView];
    
    [self.buttomScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(35+HEIGHT_NAV);
        make.right.and.bottom.equalTo(self.view);
        make.left.equalTo(self.tableV_left.mas_right);
    }];
    
}

#pragma mark - table view dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableV_left) {
        static NSString *reuseIdentifer = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifer];
            [self resetSeparatorInsetForCell:cell];
        }
     
        
        Stock *stock = self.customStocks[indexPath.row % 7];
        cell.detailTextLabel.text = stock.number;
        cell.textLabel.text = stock.title;
        return cell;
    }else{
        RightTableViewCell *cell = [RightTableViewCell cellWithTableView:tableView WithNumberOfLabels:self.rightTitles.count];
        //这里先使用假数据
        UIView *view = [cell.contentView viewWithTag:100];
        for (UILabel *label in view.subviews) {
            label.text = nil;
            label.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        }
        return cell;
    }
}
//设置cell分割线顶格
- (void)resetSeparatorInsetForCell:(UITableViewCell *)cell {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_tag_Type==1) {
        return 50;
    }else if (_tag_Type==2){
        return 2;
    }else{
        return 10;
    }
}
#pragma mark -- 设置左右两个table View的自定义头部View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == self.tableV_right) {
        UIView *rightHeaderView = [UIView viewWithLabelNumber:self.rightTitles.count];
        int i = 0;
        for (UILabel *label in rightHeaderView.subviews) {
            label.text = self.rightTitles[i++];
        }
        rightHeaderView.backgroundColor = [UIColor lightGrayColor];
        return rightHeaderView;
    }else{
        UIView *leftHeaderView = [UIView viewWithLabelNumber:1];
        [leftHeaderView.subviews.lastObject setText:@"自选"];
        leftHeaderView.backgroundColor = [UIColor lightGrayColor];
        return leftHeaderView;
    }
}
//必须实现以下方法才可以使用自定义头部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableV_right selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self.tableV_left selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

#pragma mark - 两个tableView联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableV_left) {
        [self tableView:self.tableV_right scrollFollowTheOther:self.tableV_left];
    }else{
        [self tableView:self.tableV_left scrollFollowTheOther:self.tableV_right];
    }
}

- (void)tableView:(UITableView *)tableView scrollFollowTheOther:(UITableView *)other{
    CGFloat offsetY= other.contentOffset.y;
    CGPoint offset=tableView.contentOffset;
    offset.y=offsetY;
    tableView.contentOffset=offset;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"点击了%ld----%ld",(long)indexPath.section,(long)indexPath.row);
    
    Detail_VC * VC = [[Detail_VC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark=============First_title_Delegate=============
-(void)Btn_First_title:(NSInteger)tag{
    _tag_Type=tag-100;
    [_tableV_left   reloadData];
    [_tableV_right  reloadData];
    NSLog(@"------------------------------%ld",(long)tag);
    
}

-(void)Btn_Nav_Deatil{
    
    [self.menu_left show];
    
}

@end
















































